#import "TJJ_SD_BLE_INSTRUCTIONS.h"

@implementation TJJ_SD_BLE_INSTRUCTIONS


#pragma mark -
#pragma mark 发送String指令转换成NSData类型
/*
 发送String指令转换成NSData类型
 */
+(NSData *)writeString:(NSString *)string
{//字符串格式转换成NSData类型
    NSLog(@"发送的指令：%@",string);
    int i;
    char temp[20];
    memset(temp, 0, 20);
    for (i=0;i<string.length-1;i+=2)
    {
        NSString *string2 = [string substringWithRange:NSMakeRange(i, 2)];
        temp[i/2]=strtoul([string2 UTF8String], 0, 16);
    }
    NSData *data = [NSData dataWithBytes:temp length:strlen(temp)];
    return data;
}

#pragma mark -
#pragma mark 读取硬件版本信息指令
/*
 读取硬件版本信息指令
 */
+ (void)readHardInfo
{
    AppDelegate *delegate = [[UIApplication sharedApplication]delegate];
    NSData * data = [TJJ_SD_BLE_INSTRUCTIONS writeString:SD_SEND_READER_VERSION];
    if ([delegate.writeCharacteristic.UUID.UUIDString isEqual:@"FFF2"]) {
        [delegate.peripheral writeValue:data
                      forCharacteristic:delegate.writeCharacteristic
                                   type:CBCharacteristicWriteWithResponse];
    }
    TJJLog(@"硬件版本请求");
}

#pragma mark -
#pragma mark 读取蓝牙适配器版本信息指令
/*
    读取蓝牙适配器版本信息指令
 */
+ (void)readBLEHardInfo
{
    AppDelegate *delegate = [[UIApplication sharedApplication]delegate];
    NSData * data = [TJJ_SD_BLE_INSTRUCTIONS writeString:SD_SEND_READER_BLEVERSION];
    if ([delegate.writeCharacteristic.UUID.UUIDString isEqual:@"FFF2"]) {
        [delegate.peripheral writeValue:data
                      forCharacteristic:delegate.writeCharacteristic
                                   type:CBCharacteristicWriteWithResponse];
    }
    TJJLog(@"硬件版本请求");
}

#pragma mark -
#pragma mark 读取硬件序列号信息指令
/*
 读取硬件序列号信息指令
 */
+ (void)readSerialNumber
{
    AppDelegate *delegate = [[UIApplication sharedApplication]delegate];
    NSData * data = [TJJ_SD_BLE_INSTRUCTIONS writeString:SD_SEND_READER_SERIALNUMBER];
    if ([delegate.writeCharacteristic.UUID.UUIDString isEqual:@"FFF2"]) {
        [delegate.peripheral writeValue:data
                      forCharacteristic:delegate.writeCharacteristic
                                   type:CBCharacteristicWriteWithResponse];
    }
    TJJLog(@"序列号请求");
}

/*
 解除绑定
 需要调用的几种情况：1、设置中，解绑操作(key)；2、用户绑定之后，用户登录重新打开应用或者后台进入应用出现 token失效的情况。(nokey)3、固件升级成功之后的情况。(nokey)  4、用户请求数据过程中出现的token失效，跳转到登录界面的（key）
 */
+ (void)unBind:(NSString *)string
{//需要调用的几种情况：1、设置中，解绑操作(key)；2、用户绑定之后，用户登录重新打开应用或者后台进入应用出现 token失效的情况。(nokey)3、固件升级成功之后的情况。(key)  4、用户请求数据过程中出现的token失效，跳转到登录界面的（key）
    TJJLog(@"解绑操作进行中。。。");
    AppDelegate *delegate = [[UIApplication sharedApplication]delegate];
    UserConfig *config = [UserConfig sharingUserConfig];
    delegate.myRemoveConnect = YES;//标记是用户自己点击的解除绑定

    if (delegate.peripheral != nil && ![TJJDefines isNull:[config getBluetoothKey]])
    {//如果外设不为空，并且key存在
        
        TJJLog(@"解绑-------key");
        NSData *data  = [TJJ_SD_BLE_INSTRUCTIONS writeString:SD_SEND_REMOVE_BINDING];//下位机断开连接
        [delegate.peripheral writeValue:data
                      forCharacteristic:delegate.writeCharacteristic
                                   type:CBCharacteristicWriteWithResponse];
    }
    if (delegate.Bluetooth)
    {//如果是已经中断连接了的，不需要再执行
        [delegate.mgr disConnect];//断开连接
        TJJLog(@"断开连接断开连接断开连接断开连接断开连接");
    }
    
    [config setMarkNewNoSendKey:NO];//设置标记 1.4 以下的为NO
    delegate.Bluetooth = NO;
    delegate.myRemoveConnect = YES;
    //本地绑定设备信息修改。
    [config setConnectCool:@"0"];//设定绑定状态为0.解绑状态
    [config setBlueToothKey:nil];//清除key
    [config setBluetoothAddress:nil];//清除UUID
    [config setSerialNumber:@"0"];
    [config setHardware:@"0"];
    
    if ([string isEqualToString:@"key"])
    {//如果是key
        [[NSNotificationCenter defaultCenter] postNotificationName:@"headRed" object:nil userInfo:nil];//通知，解绑状态
        //发送电量改变的通知  电池断开
        NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity:0];
        [dict setObject:@"NO" forKey:@"disConnect"];//连接状态修改为NO，
        [[NSNotificationCenter defaultCenter] postNotificationName:@"power" object:nil userInfo:dict];//通知，改变电量显示方案
    }
}
@end
